
#include "softmax_tiling.h"
#include "register/op_def_registry.h"


namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  SoftmaxTilingData tiling;
    auto shape_x = context->GetInputShape(0)->GetOriginShape();

    int32_t x_ndarray[20];
    int32_t x_dimensional;

    x_dimensional = shape_x.GetDimNum();
    for(int i = 0; i < x_dimensional; i++)
    {
        x_ndarray[i] = shape_x.GetDim(i);
    }

    int32_t dim = *context->GetAttrs()->GetInt(0);
    if(dim < 0) dim += x_dimensional;

    int32_t cycles = 1;
    int32_t interval = 1;
    int32_t loopCount = 1;

    for(int i = 0; i < x_dimensional; i++)
    {
        loopCount *= x_ndarray[i];
    }
    loopCount = loopCount / x_ndarray[dim];
    
    cycles = x_ndarray[dim];
    
    for(int i = dim+1; i < x_dimensional; i++)
    {
        interval *= x_ndarray[i];
    }

    if(interval == 1)
    {
        constexpr uint32_t SHARED_TMP_BUFFER_SIZE = 61440; // reserved tmpbuffer 60K for softmax compute
        context->SetTilingKey(2);

        int32_t colNum = cycles;
        int32_t rowNum = loopCount;

        uint32_t localworkspaceSize = SHARED_TMP_BUFFER_SIZE;
        
        ge::Shape softmaxComputeShape({ 32, colNum });
        AscendC::SoftMaxTilingFunc(softmaxComputeShape, sizeof(float), localworkspaceSize, tiling.softmaxTilingData);

        //matmul_tiling::half

        tiling.set_cycles(cycles);
        tiling.set_interval(localworkspaceSize);
        tiling.set_loopCount(loopCount);
    }
    else
    {
        tiling.set_cycles(cycles);
        tiling.set_interval(interval);
        tiling.set_loopCount(loopCount);

        context->SetTilingKey(1);
    }


    

    context->SetBlockDim(1);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    return ge::GRAPH_SUCCESS;

  return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Softmax : public OpDef {
public:
    explicit Softmax(const char* name) : OpDef(name)
    {
        Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        Attr("dim").AttrType(OPTIONAL).Int(-1);

        SetInferShape(ge::InferShape);

        AICore()
            .SetTiling(optiling::TilingFunc);
        AICore().AddConfig("ascend310b");

    }
};

OP_ADD(Softmax);
}
